package com.onenine.chat.rongcloud;

import android.app.Activity;
import android.content.SharedPreferences;
import android.text.TextUtils;

import com.onenine.app.response.UserInfoResponse;
import com.onenine.app.utils.DataType;
import com.onenine.common.util.SPUtils;
import com.umeng.share.UMLogin;
import com.umeng.socialize.bean.SHARE_MEDIA;

/**
 * 创建时间：2018/5/31 0031 10:09
 * 创建人：fly
 * 功能描述：登录及IM用户
 */
public class RcUser {

    private static RcUser rcUser;
    private String userId, userPkId;
    private String token;
    private int loginType, sex;
    private UserInfoResponse infoResponse;

    protected RcUser() {
    }

    public static RcUser getInstance() {
        if (rcUser == null) {
            rcUser = new RcUser();
        }
        return rcUser;
    }

    public String getUserPkId() {
        if (TextUtils.isEmpty(userPkId)) {
            userPkId = SPUtils.getInstance().getString("userPkId");
        }
        return userPkId;
    }

    public String getUserId() {
        if (TextUtils.isEmpty(userId)) {
            userId = SPUtils.getInstance().getString("userId");
        }
        return userId;
    }

    public int getLoginType() {
        if (loginType == 0) {
            loginType = SPUtils.getInstance().getInt("loginType");
        }
        return loginType;
    }

    public String getToken() {
        if (TextUtils.isEmpty(token)) {
            token = SPUtils.getInstance().getString("token");
        }
        return token;
    }

    public int getSex() {
        if (sex == 0) {
            sex = SPUtils.getInstance().getInt("sex");
            if (sex == 0 && infoResponse != null) {
                sex = infoResponse.getSex();
            }
        }
        return sex;
    }

    public RcUser setUserId(String userId) {
        this.userId = userId;
        return this;
    }

    public RcUser setUserPkId(String userPkId) {
        this.userPkId = userPkId;
        return this;
    }

    public RcUser setSex(int sex) {
        if (sex > 0) {
            this.sex = sex;
        }
        return this;
    }

    public RcUser setToken(String token) {
        this.token = token;
        return this;
    }

    public RcUser setLoginType(int type) {
        loginType = type;
        return this;
    }

    public UserInfoResponse getInfoResponse() {
        return infoResponse;
    }

    public void setInfoResponse(UserInfoResponse infoResponse) {
        this.infoResponse = infoResponse;
    }

    /**
     * 缓存用户信息
     */
    public void applyUser() {
        SharedPreferences.Editor editor = SPUtils.getInstance().getEditor();
        if (!TextUtils.isEmpty(userId)) {
            editor.putString("userId", userId);
        }
        if (!TextUtils.isEmpty(userPkId)) {
            editor.putString("userPkId", userPkId);
        }
        if (!TextUtils.isEmpty(token)) {
            editor.putString("token", token);
        }
        if (loginType > 0) {
            editor.putInt("loginType", loginType);
        }
        if (sex > 0) {
            editor.putInt("sex", sex);
        }
        editor.apply();
    }

    /**
     * 清除用户信息
     */
    public void clearUser() {
        userId = null;
        userPkId = null;
        token = null;
        loginType = 0;
        sex = 0;
        SPUtils.getInstance().clearAll();
        UserCacheMgr.getInstance().deleteAllUsers();
    }

    /**
     * 删除第三方授权缓存
     *
     * @param activity
     */
    public void deleteAuthorize(Activity activity) {
        if (loginType == DataType.QQ_LOGIN) {
            UMLogin.deleteAuthorize(activity, SHARE_MEDIA.QQ, null);
        } else if (loginType == DataType.WEIXIN_LOGIN) {
            UMLogin.deleteAuthorize(activity, SHARE_MEDIA.WEIXIN, null);
        }
    }

}
